#pragma once

#include"Log.hpp"
#include<iostream>
#include<memory>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<strings.h>

using namespace std;

extern Log log;

enum{
    SOCKET_ERR = 1
};

string defaultip = "0.0.0.0";
uint64_t defaultport = 8000;
class UdpServer
{
public:
    UdpServer(const uint16_t &port = defaultport, const string &ip = defaultip)
    :_port(port)
    ,_ip(ip)
    {}
    
    void Init()
    {
        //打开网卡
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(_sockfd < 0)
        {
            log(Fatal, "socket creat error, sockfd : %d", _sockfd);
            exit(SOCKET_ERR);
        }
        log(Info, "socket creat success, sockfd : %d", _sockfd);

        //bind socket
        struct sockaddr_in local;
        bzero(&local, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = _port;
        local.sin_addr = _ip;
    }

    void Run()
    {}

private:
    int _sockfd;//网络文件描述符
    string _ip;//ip?
    uint16_t _port;//端口号
};